using System;
using System.IO;

namespace kerberosLibs
{
	/// <summary>
	/// Summary description for TaskKInit.
	/// </summary>
	public class TaskKInit : LoggingConnectionTask
	{
		public TaskKInit()
		{
		}

		/// <summary>
		/// How many steps will we take?
		/// </summary>
		public override int NumberOfSteps
		{
			get
			{
				return 4;
			}
		}

		/// <summary>
		/// The password we should use to establish a kerberos linkage.
		/// </summary>
        public string KerberosPassword { get; set; }

		/// <summary>
		/// The principle name to get the kerberos ticket for!
		/// </summary>
        public string KerberosPrinciple { get; set; }

		/// <summary>
		/// Start the kinit process!
		/// </summary>
		public override void Start()
		{
			FileInfo kinit_exe = kerberosLibs.kerberosProductAccess.kinitPath;
			string kinit_args = "-f";

            if (KerberosPrinciple != "") 
			{
                kinit_args += " " + KerberosPrinciple;
			}

			InitProcess (kinit_exe, kinit_args);
			_proc.OnProcessExit += new kerberosLibs.ProcessWatcher.ProcessExit(_proc_OnProcessExit);

			///
			/// Ready to start the process!
			/// 

			OnUIStatusUpdate (0, "Starting kinit");
			StartProcess();
			OnUIStatusUpdate (1, "Sending password");
            _proc.StdInput.Write(KerberosPassword + "\n");
			OnUIStatusUpdate (2, "Waiting for authentication");
			_proc.NoFurtherInput();
		}

		/// <summary>
		/// When the kinit process finishes, we are outta here! We are done!
		/// </summary>
		private void _proc_OnProcessExit()
		{
			OnUIStatusUpdate (3, "Kerberos Principle Aquired");
			OnTaskDone ();
		}
	}
}
